// pages/details/details.js
import {URI_PARTWORKDETAILS2, URI_PARTWORKDETAILS3,URI_PARTWORKSUBLIFE} from "../../utils/api";
import {ajax} from "../../utils/httpRequest";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // options.type=4;
    options = JSON.parse(options.item);
    // console.log(options);
    console.log(options.type);
    if (options.type !== 4) {
      ajax.post(URI_PARTWORKDETAILS2, {
        branchs_work_id: options.work_id,
        uid: wx.getStorageSync('uid')
      }).then(res => {
        // res.data.type=4;
        this.setData({
          item: res.data
        })
      }).catch(error => {
        console.log(error);
      });
    } else {
      ajax.post(URI_PARTWORKDETAILS3, {
        //72 state = 3
        //71 为完成 state=2
        //

        //branchs_work_id: options.work_id,
        branchs_work_id: options.work_id
      }).then(res => {
        this.setData({
          item: res.data
        })
      }).catch(error => {
        console.log(error);
      });
    }
  },
  readfile(e) {
    let url = e.currentTarget.dataset.url;
    console.log(url);
    if (url) {
      wx.downloadFile({
        url: url,
        success(res) {
          const filePath = res.tempFilePath;
          wx.openDocument({
            filePath,
            success(res) {
              console.log('打开文档成功')
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '暂无文件',
        icon: 'none',
      })
    }

  },
  submitBtn(e){
    let img_path = e.detail.imgList.join('#');
    let id = this.data.item.id;
    let that= this;
    ajax.post(URI_PARTWORKSUBLIFE,{
      id:id,
      img_path:img_path
    }).then(res=>{
      // console.log(res.msg)
      wx.showModal({
        title: "温馨提示",
        content: res.msg,
        showCancel: false,
        confirmColor: '#0d77e4',
        success: function (res) {
          if (res.confirm) {
            ajax.post(URI_PARTWORKDETAILS3, {
              branchs_work_id: id
            }).then(res => {
              that.setData({
                item: res.data
              })
            }).catch(error => {
              console.log(error);
            });
          }
        }
      })
    })
  }
})